package com.ohos.connent.bizscenecloud.biz.modular.dp.utils;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.context.model.SaRequest;
import com.aliyun.tea.utils.StringUtils;
import com.ohos.connent.bizscenecloud.common.pojo.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: ChenJiaHao
 * @Date: 2024/6/5 14:21
 * @Description:
 */
@Slf4j
@Component
public class RestTemplateHelper {

    @Value("${modelcloud.api.model.biz.path.prefix}")
    private String modelCloudPath;

    @Resource(name = "defaultRestTemplate")
    private RestTemplate restTemplate;

    public CommonResult getProCategoryTree(String searchKey) {
        String path = modelCloudPath + "/pro-category-dict/tree";
        // 请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        SaRequest request = SaHolder.getRequest();
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            token = request.getCookieValue("token");
            log.info(">>> 从cookie中获取token");
        } else {
            log.info(">>> 从请求头中获取token");
        }
        headers.set("token", token);
        // 请求体
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("searchKey", searchKey);
        //构造实体对象
        HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(requestBody, headers);
        try {
            ResponseEntity<CommonResult> responseEntity = restTemplate.postForEntity(path, httpEntity, CommonResult.class);
            HttpStatus httpStatus = responseEntity.getStatusCode();
            if (httpStatus.equals(HttpStatus.OK)) {
                return responseEntity.getBody();
            }

            log.error(">>> 调用远程服务接口失败，响应结果：{}", responseEntity);
            return CommonResult.error("请求接口失败 :" + path);
        } catch (Exception e) {
            return CommonResult.error("调用远程服务器时发生异常");
        }
    }

    public CommonResult getCategoryDictDetail(String categoryDictId) {
        String path = modelCloudPath + "/pro-category/detailByDictId/?id=" + categoryDictId ;
        // 请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        SaRequest request = SaHolder.getRequest();
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            token = request.getCookieValue("token");
            log.info(">>> 从cookie中获取token");
        } else {
            log.info(">>> 从请求头中获取token");
        }
        headers.set("token", token);

        //构造实体对象
        HttpEntity<String> httpEntity = new HttpEntity<>("parameters",headers);
        try {
            ResponseEntity<CommonResult> responseEntity = restTemplate.exchange(path, HttpMethod.GET,httpEntity,CommonResult.class );
            HttpStatus httpStatus = responseEntity.getStatusCode();
            if (httpStatus.equals(HttpStatus.OK)) {
                return responseEntity.getBody();
            }

            log.error(">>> 调用远程服务接口失败，响应结果：{}", responseEntity);
            return CommonResult.error("请求接口失败 :" + path);
        } catch (Exception e) {
            return CommonResult.error("调用远程服务器时发生异常");
        }
    }

    public CommonResult getProFile(String fileId) {
        String path = modelCloudPath + "biz/profile/file/getFileDownloadAuthUrlHUAWEI";
        // 请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        SaRequest request = SaHolder.getRequest();
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            token = request.getCookieValue("token");
            log.info(">>> 从cookie中获取token");
        } else {
            log.info(">>> 从请求头中获取token");
        }
        headers.set("token", token);

        // 请求体
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("fileId", fileId);

        //构造实体对象
        HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(requestBody, headers);
        try {
            ResponseEntity<CommonResult> responseEntity = restTemplate.postForEntity(path,httpEntity,CommonResult.class);
            HttpStatus httpStatus = responseEntity.getStatusCode();
            if (httpStatus.equals(HttpStatus.OK)) {
                return responseEntity.getBody();
            }

            log.error(">>> 调用远程服务接口失败，响应结果：{}", responseEntity);
            return CommonResult.error("请求接口失败 :" + path);
        } catch (Exception e) {
            return CommonResult.error("调用远程服务器时发生异常");
        }
    }
}
